shell脚本

Created at 2019-03-25 Updated at 2019-08-15 Category Linux基础 Tag shell脚本


shell脚本

1.shell特性 :

shell : GUI  graphical user interface  图形用户界面
         CLI  command line interface  命令行界面 

shell 类型: bash  sh  csh tcsh zsh

bash特点:

1.命令补全 : tab 
        命令补全: PATH 
        路径补全:  

2. 命令别名: 
        alias  
        alias mm='命令'

    取消别名: unalias 

3. 光标的快速移动
    ctrl + A  : 快速移到行首
    ctrl + E  : 快速移到行尾
    ctrl + U  : 光标前面内容删除
    ctrl + K  : 光标后面内容删除

    ctrl + 左右键  : 按单词去跳 

shell脚本: .sh

#!/bin/bash (第一行)
脚本默认解释器 /bin/bash

执行方式:
bash + 脚本名字
./test.sh (加上执行权限)
chmod +x test.sh

静态编译型语言: c
编译器 二进制可执行文件
看不到源码
脚本语言 : python perl 解释器 能看到源码

shell传参 :
$0 : 程序名
$1 : 第一个参数
$2 : 第二个参数

shell变量的命名:

a=100

1. 数字字母下划线组成,不能以数字开头
    123a=100(X)
    ?abc=100 (X)

    ab_123 对的示范

2. 变量名和等号之间不能有空格 
    abc = 100(X)

    abc=100

3. 不能用bash的特殊字 
    help  

使用变量: $变量名

let : 数学运算

let result=a+b

test : 判断
-eq : 相等 equal
-lt : 小于 less than
-le : 小于等于 less equal
-gt : 大于 greater than
-ge : 大于等于 greater equal
-ne : 不等于 not equal

if 条件; then
执行命令
else
执行其他命令
fi

if 条件
then
执行命令
elif 条件
执行
else
执行
fi

Table of Content

  1. shell脚本
Site by csy using Hexo & Random

Hide